package com.morizhang.merchanmanager.api.plugins.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.morizhang.merchanmanager.api.bean.StatisticDataHolder;
import com.morizhang.merchanmanager.api.dao.CommodityDataDao;
import com.morizhang.merchanmanager.api.dao.CommoditySellingDao;
import com.morizhang.merchanmanager.api.dao.CommodityStorageDao;
import com.morizhang.merchanmanager.api.dao.PriceInfoDao;
import com.morizhang.merchanmanager.api.model.*;
import com.morizhang.merchanmanager.api.plugins.BasicSaleManage;
import com.morizhang.merchanmanager.api.plugins.PluginAnnotation;
import com.morizhang.merchanmanager.api.utils.SerialNoGenerator;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@PluginAnnotation(name = "BasicSaleManage")
public class BasicSaleManageImpl implements BasicSaleManage {
    @Autowired
    CommodityStorageDao commodityStorageDao;

    @Autowired
    PriceInfoDao priceInfoDao;

    @Autowired
    CommodityDataDao commodityDataDao;

    @Override
    public void doSale(JSONArray saleList) {
        if (saleList == null || saleList.isEmpty())
            return;
        List<SaleModel> saleListFormed = new ArrayList<>();
        for (Iterator i = saleList.stream().iterator(); i.hasNext(); ) {
            JSONObject singleSaleRecord = (JSONObject) i.next();
            SaleModel singleSaleModel = JSON.toJavaObject(singleSaleRecord, SaleModel.class);
            saleListFormed.add(singleSaleModel);
        }

        for (SaleModel singleSaleModel : saleListFormed) {
            CommodityLocationModel commodityLocationModel = new CommodityLocationModel(singleSaleModel);
            List<CommodityLocationModel> commodityShopLocation = commodityStorageDao.queryCommodityShopLocation(commodityLocationModel);
            if (commodityShopLocation == null || commodityShopLocation.isEmpty())
                return;
            CommodityLocationModel commodityShopReadyUpdate = commodityShopLocation.get(0);
            int commodityShopAmountNow = commodityShopReadyUpdate.getAmountNow();
            if (commodityShopAmountNow < singleSaleModel.getAmount())
                return;
            if (commodityShopAmountNow == singleSaleModel.getAmount()) {
                commodityStorageDao.deleteCommodityShopLocation(commodityShopReadyUpdate.getSerialNo());
            }
            if (commodityShopAmountNow > singleSaleModel.getAmount()) {
                commodityShopReadyUpdate.setAmountNow(commodityShopAmountNow - singleSaleModel.getAmount());
                commodityStorageDao.updateCommodityShopLocation(commodityShopReadyUpdate);
            }

            //更新货品总库存-当存在多个批次的该产品时，默认减少批次号最小的
            List<StockModel> allStorage = commodityStorageDao.queryCommodityStorageByBarcode(singleSaleModel.getBarCode());
            String minBatchNo = allStorage.get(0).getBatchNo();
            StockModel minBatchNoRecord = allStorage.get(0);
            for (StockModel singleStorageRecord : allStorage) {
                String nowBatchNo = singleStorageRecord.getBatchNo();
                if (nowBatchNo.compareTo(minBatchNo) < 0) {
                    minBatchNo = nowBatchNo;
                    minBatchNoRecord = singleStorageRecord;
                }
            }
            if (minBatchNoRecord != null) {
                int storageAmountNow = minBatchNoRecord.getAmountNow();
                if (storageAmountNow < singleSaleModel.getAmount())
                    return;
                if (storageAmountNow == singleSaleModel.getAmount()) {
                    commodityStorageDao.deleteCommodityStorage(minBatchNoRecord.getSerialNo());
                }
                if (storageAmountNow > singleSaleModel.getAmount()) {
                    minBatchNoRecord.setAmountNow(storageAmountNow - singleSaleModel.getAmount());
                    commodityStorageDao.updateCommodityStorage(minBatchNoRecord);
                }
            }
        }

        StatisticDataHolder.makeExpire("storageValue");
        StatisticDataHolder.makeExpire("storageSumNum");
    }

    @Override
    public PriceModel queryCommoditySellPrice(String barCode) {
       PriceModel searchResult =priceInfoDao.queryCommoditySellPriceByBarcode(barCode);
       CommodityDataModel commodityData = commodityDataDao.queryCommodityByBarcode(searchResult.getBarCode());
       searchResult.setProductName(commodityData.getProductName());
       return searchResult;
    }

    @Override
    public List<PriceModel> selectAllCommoditySellPrice() {
        List<PriceModel> searchResult = priceInfoDao.selectAllCommoditySellPrice();
        for (PriceModel singleResult : searchResult) {
            CommodityDataModel commodityData = commodityDataDao.queryCommodityByBarcode(singleResult.getBarCode());
            singleResult.setProductName(commodityData.getProductName());
        }
        return searchResult;
    }

    @Override
    public void addCommoditySellPrice(PriceModel priceModel) {
        priceInfoDao.addCommoditySellPrice(priceModel);
    }

    @Override
    public void deleteCommoditySellPrice(String barCode) {
        priceInfoDao.deleteCommoditySellPrice(barCode);
    }

    @Override
    public void updateCommoditySellPrice(PriceModel priceModel) {
        priceInfoDao.updateCommoditySellPrice(priceModel);
    }

    @Autowired
    CommoditySellingDao commoditySellingDao;

    @Override
    public List<CommoditySellingModel> selectAllCommoditySelling() {
        List<CommoditySellingModel> queryResult = commoditySellingDao.selectAllCommoditySelling();
        for (CommoditySellingModel singleResult : queryResult) {
            CommodityDataModel commodityData = commodityDataDao.queryCommodityByBarcode(singleResult.getBarCode());
            singleResult.setProductName(commodityData.getProductName());
        }
        return queryResult;
    }

    @Override
    public void addCommoditySelling(CommoditySellingModel commoditySellingModel) throws Exception {
        if (commoditySellingModel.getSerialNo() == null)
            commoditySellingModel.setSerialNo(SerialNoGenerator.newSerialNo());
        commoditySellingDao.addCommoditySelling(commoditySellingModel);
    }

    @Override
    public void deleteCommoditySelling(String serialNo) {
        commoditySellingDao.deleteCommoditySellingModel(serialNo);
    }

    @Override
    public void updateCommoditySelling(CommoditySellingModel commoditySellingModel) {
        commoditySellingDao.updateCommoditySellingModel(commoditySellingModel);
    }



    @Override
    public void doReturnedPurchase(JSONArray returnedList) {

    }

    @Override
    public boolean checkSelling(String barCode) {
        CommodityLocationModel queryParam = new CommodityLocationModel();
        queryParam.setBarCode(barCode);
        List <CommodityLocationModel> searchResult = commodityStorageDao.queryCommodityShopLocation(queryParam);
        return searchResult != null && !searchResult.isEmpty();
    }
}
